package cn.com.dpc;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.dpc.adapter.ColumnAdapter;
import cn.com.dpc.adapter.MixAdapter;
import cn.com.dpc.adapter.TableAdapter;
import cn.com.dpc.adapter.TableAdapter.TableCell;
import cn.com.dpc.adapter.TableAdapter.TableRow;
import cn.com.dpc.bean.Columnobject;
import cn.com.dpc.util.HttpGet;

public class ParamsActivityOld extends Activity {
	private int stationId;
	private int devId;
	private String devName = "";
	private String resultStr = "";

	ListView lv;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.paramsold);

		Bundle bundle = getIntent().getExtras();
		String url = bundle.getString("url");
		devName = bundle.getString("text");
		stationId = Integer.parseInt(url.split("_")[0]);
		devId = Integer.parseInt(url.split("_")[1]);

		ImageButton editButton = (ImageButton) findViewById(R.id.edit_button);
		editButton.setVisibility(View.INVISIBLE);
		ImageButton backButton = (ImageButton) findViewById(R.id.back_button);
		backButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				ParamsActivityOld.this.finish();
			}
		});

		TextView titleText = (TextView) findViewById(R.id.myTitle);
		titleText.setText(devName);

		lv = (ListView) this.findViewById(R.id.ListView01);
		// ArrayList<TableRow> table = new ArrayList<TableRow>();
		// TableCell[] titles = new TableCell[3];// 每行5个单元
		// int width = this.getWindowManager().getDefaultDisplay().getWidth()
		// / titles.length;
		// // 定义标题
		// for (int i = 0; i < titles.length; i++) {
		// titles[i] = new TableCell("标题" + String.valueOf(i), width + 8 * i,
		// LayoutParams.FILL_PARENT, TableCell.STRING);
		// }
		// table.add(new TableRow(titles));
		// // 每行的数据
		// TableCell[] cells = new TableCell[3];// 每行5个单元
		// for (int i = 0; i < cells.length; i++) {
		// cells[i] = new TableCell("No." + String.valueOf(i),
		// titles[i].width, LayoutParams.FILL_PARENT, TableCell.STRING);
		// }
		// // 把表格的行添加到表格
		// for (int i = 0; i < 12; i++)
		// table.add(new TableRow(cells));
		// TableAdapter tableAdapter = new TableAdapter(this, table);
		// lv.setAdapter(tableAdapter);
		lv.setOnItemClickListener(new ItemClickEvent());

		showList();

	}

	class ItemClickEvent implements AdapterView.OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			Toast.makeText(ParamsActivityOld.this,
					"选中第" + String.valueOf(arg2) + "行", 500).show();
		}
	}

	private void showList() {
		new AsyncTask<Void, Void, Boolean>() {

			@Override
			protected Boolean doInBackground(Void... arg0) {
				try {
					resultStr = HttpGet.getResult(
							"http://218.249.218.105/realData?method=fxLoadRealData&stationno="
									+ stationId + "&deviceno=" + devId,
							ParamsActivityOld.this, "utf8");
					Log.v("dpc.ParamsActivity.resultStr", resultStr);
					return true;
				} catch (Exception e) {
					e.printStackTrace();
				}
				return false;
			}

			@Override
			protected void onPostExecute(Boolean result) {
				super.onPostExecute(result);
				closeProgressBar();
				if (result) {

					try {
						ArrayList<TableRow> table = new ArrayList<TableRow>();
						TableCell[] titles = new TableCell[3];// 每行3个单元
						int width = ParamsActivityOld.this.getWindowManager()
								.getDefaultDisplay().getWidth()
								/ titles.length;
						// 定义标题
						titles[0] = new TableCell("参量名", width,
								LayoutParams.FILL_PARENT, TableCell.STRING);
						titles[1] = new TableCell("参量值", width,
								LayoutParams.FILL_PARENT, TableCell.STRING);
						titles[2] = new TableCell("时间", width * 2,
								LayoutParams.FILL_PARENT, TableCell.STRING);
						table.add(new TableRow(titles));
						// 每行的数据
						JSONObject jsonObject = new JSONObject(resultStr);
						JSONArray jsonArray = jsonObject
								.getJSONArray("airealdata");
						Log.v("dpc.ParamsActivity.jsonArrayLength",
								jsonArray.length() + " ");
						for (int i = 0; i < jsonArray.length(); i++) {
							JSONObject jo = jsonArray.getJSONObject(i);

							TableCell[] cells = new TableCell[3];
							cells[0] = new TableCell(jo.getString("name"),
									titles[0].width, LayoutParams.FILL_PARENT,
									TableCell.STRING);
							cells[1] = new TableCell(jo.getString("value"),
									titles[1].width, LayoutParams.FILL_PARENT,
									TableCell.STRING);
							cells[2] = new TableCell(jo.getString("date"),
									titles[2].width, LayoutParams.FILL_PARENT,
									TableCell.STRING);

							table.add(new TableRow(cells));
						}

						TableAdapter tableAdapter = new TableAdapter(
								ParamsActivityOld.this, table);
						lv.setAdapter(tableAdapter);

					} catch (Exception e) {
						// TODO: handle exception
					}

				} else {
					Toast.makeText(ParamsActivityOld.this, "数据加载失败！",
							Toast.LENGTH_SHORT).show();
				}

			}

			@Override
			protected void onPreExecute() {
				super.onPreExecute();
				showProgressBar();
			}

		}.execute();

	}

	private void changeObject(List<Columnobject> list,
			ColumnAdapter columnAdapter) {
		int[] number = new int[list.size()];
		int[] type = new int[list.size()];
		String[] url = new String[list.size()];
		String[] texts = new String[list.size()];
		for (int i = 0; i < list.size(); i++) {
			Columnobject columnobject = list.get(i);
			number[i] = columnobject.getNumber();
			type[i] = columnobject.getType();
			url[i] = columnobject.getUrl();
			texts[i] = columnobject.getTexts();
		}
		columnAdapter.setNumber(number);
		columnAdapter.setType(type);
		columnAdapter.setUrl(url);
		columnAdapter.setTexts(texts);
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			this.finish();
			return true;
		}
		return true;
	}

	public void showProgressBar() {
		AnimationSet set = new AnimationSet(true);

		Animation animation = new AlphaAnimation(0.0f, 1.0f);
		animation.setDuration(500);
		set.addAnimation(animation);

		animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
				Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
				-1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
		animation.setDuration(500);
		set.addAnimation(animation);

		LayoutAnimationController controller = new LayoutAnimationController(
				set, 0.5f);
		RelativeLayout loading = (RelativeLayout) findViewById(R.id.loading);
		loading.setVisibility(View.VISIBLE);
		loading.setLayoutAnimation(controller);
	}

	public void closeProgressBar() {

		AnimationSet set = new AnimationSet(true);

		Animation animation = new AlphaAnimation(0.0f, 1.0f);
		animation.setDuration(500);
		set.addAnimation(animation);

		animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
				Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
				0.0f, Animation.RELATIVE_TO_SELF, -1.0f);
		animation.setDuration(500);
		set.addAnimation(animation);

		LayoutAnimationController controller = new LayoutAnimationController(
				set, 0.5f);
		RelativeLayout loading = (RelativeLayout) findViewById(R.id.loading);

		loading.setLayoutAnimation(controller);

		loading.setVisibility(View.INVISIBLE);
	}

	public void showProgressBar(String title) {
		TextView loading = (TextView) findViewById(R.id.txt_loading);
		loading.setText(title);
		showProgressBar();
	}
}
